package com.chahu.servlet.front;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.chahu.common.Result;
import com.chahu.service.UserService;
import com.chahu.service.impl.UserServiceImpl;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    UserService service;

    @Override
    public void init() throws ServletException {
        service = new UserServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json; charset=UTF-8");

        try (PrintWriter writer = resp.getWriter()) {
            Cookie[] cookies = req.getCookies();
            if (cookies != null) {
                String username = null;
                String password = null;
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("username")) username = cookie.getValue();
                    if (cookie.getName().equals("password")) password = cookie.getValue();
                }
                if (username != null && password != null) {
                    if (service.auth(username, password, req.getSession())) {
                        writer.write(JSON.toJSONString(Result.success("登陆成功")));
                        writer.close();
                        return;
                    }
                }
            }
            writer.write(JSON.toJSONString(Result.error("登陆失败")));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json; charset=UTF-8");
        PrintWriter writer = resp.getWriter();

        String json = IOUtils.toString(req.getReader());

        JSONObject data = JSONObject.parseObject(json);

        String username = data.getString("username");
        String password = data.getString("password");
        boolean remember = data.getBoolean("remember");

        if(username.isEmpty() || password.isEmpty()){
            writer.write(JSON.toJSONString(Result.error("用户名或密码错误")));
            writer.close();
            return;
        }

        boolean auth = service.auth(username, password, req.getSession());
        if(auth){
//            if(remember){
                Cookie cookie_username = new Cookie("username", username);
                Cookie cookie_password = new Cookie("password", password);
                cookie_username.setMaxAge(60 * 60 * 24 * 5);
                cookie_password.setMaxAge(60 * 60 * 24 * 5);
                resp.addCookie(cookie_username);
                resp.addCookie(cookie_password);
//            }
            writer.write(JSON.toJSONString(Result.success(username)));
            System.out.println(JSON.toJSONString(Result.success(username)));
        }else {
            this.doGet(req, resp);
        }
        writer.close();
    }
}
